/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun<iot25@lingyun>
 *                  All rights reserved.
 *
 *       Filename:  thread.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(24/04/25)
 *         Author:  LingYun <iot25@lingyun>
 *      ChangeLog:  1, Release initial version on "24/04/25 14:31:40"
 *                 
 ********************************************************************************/

#include "thread.h"


int thread_client(pthread_t *pid, Thread_Body thread_worker, void *arg)
{
    int rv = -1;
    pthread_attr_t thread_attr;
    
    // 初始化线程属性
    if (pthread_attr_init(&thread_attr)) {
        fprintf(stderr, "Pthread_attr_init failure: %s\n", strerror(errno));
        goto cleanup;
    }
    
    // 设置线程栈大小
    if (pthread_attr_setstacksize(&thread_attr, 120 * 1024)) {
        fprintf(stderr, "Pthread_attr_setstacksize failure: %s\n", strerror(errno));
        goto cleanup;
    }
    
    // 设置线程分离状态
    if (pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) {
        fprintf(stderr, "Pthread_attr_setdetachstate failure: %s\n", strerror(errno));
        goto cleanup;
    }
    
    // 创建线程
    if (pthread_create(pid, &thread_attr, thread_worker, arg)) {
        fprintf(stderr, "Pthread_create failure: %s\n", strerror(errno));
        goto cleanup;
    }
    
    rv = 0;
    
cleanup:
    pthread_attr_destroy(&thread_attr);
    return rv;
}

